package team.seekers.hula.question.answerComment.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import team.seekers.hula.question.answerComment.entity.dao.AnswerCommentGoodDO;
import team.seekers.hula.question.answerComment.mapper.AnswerCommentGoodMapper;
import team.seekers.hula.auth.service.MindUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 给评论点赞 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-02-19
 */
@Service
public class AnswerCommentGoodService extends ServiceImpl<AnswerCommentGoodMapper, AnswerCommentGoodDO> {

    private boolean result;

    @Autowired
    private AnswerCommentService answerCommentService;

    public void changeGood(String answerCommentId) {
        if(hasGood(answerCommentId)){
            // 维护评论赞数
            answerCommentService.reduceGood(answerCommentId);

            // 删除赞
            QueryWrapper<AnswerCommentGoodDO> wrapper = new QueryWrapper<>();
            wrapper.eq("answer_comment_id", answerCommentId);
            remove(wrapper);
        }else {
            // 维护评论赞数
            answerCommentService.addGood(answerCommentId);

            // 新增赞
            AnswerCommentGoodDO answerCommentGoodDO = new AnswerCommentGoodDO();
            answerCommentGoodDO.setAnswerCommentId(answerCommentId);
            answerCommentGoodDO.setUserId(MindUtils.getUserId());
            save(answerCommentGoodDO);
        }
    }

    //判断是否已经点过赞了，true为已经点过了
    public boolean hasGood(String answerCommentId){
        QueryWrapper<AnswerCommentGoodDO> wrapper = new QueryWrapper<>();
        wrapper.eq("answer_comment_id", answerCommentId);
        wrapper.eq("user_id", MindUtils.getUserId());
        AnswerCommentGoodDO answerCommentGoodDO = getOne(wrapper);
        if(answerCommentGoodDO == null){
            return false;
        }else {
            return true;
        }
    }

    public void deleteByAnswerCommentId(String id) {
        QueryWrapper<AnswerCommentGoodDO> wrapper = new QueryWrapper<>();
        wrapper.eq("answer_comment_id",id);
        remove(wrapper);
    }
}
